package com.dhecp.project.hr.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.hr.domain.AttendanceWorkDateSet;
import com.dhecp.project.hr.service.AttendanceWorkDateSetService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 工作时间设定 控制层
 * @author lwl
 * @version 2020年5月22日
 */
@Controller
@RequestMapping("/hr/attendanceworkdateset")
public class AttendanceWorkDateSetController extends BaseModuleController{

	private String prefix = "hr/attendanceworkdateset";
    private String moduleTable = ModuleTable.ATTENDANCE_WORK_DATE_SET;
    
    @Autowired
    private AttendanceWorkDateSetService workDateSetService;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
    
    /**
     * 打开列表
     * @param 
     * @return ModelAndView
     * @author lwl
     */
	@RequiresPermissions("hr:attendanceworkdateset:view")
    @GetMapping()
    public ModelAndView dataList(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new AttendanceWorkDateSet()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	Integer toolBarWidth = 0;
    	Subject userSubject = ShiroUtils.getSubject();
    	if (userSubject.isPermitted("hr:attendanceworkdateset:edit")){ //当前登录用户主体是否有修改的权限
    		toolBarWidth = toolBarWidth + 180;
    	}
    	params.put("toolBarWidth", toolBarWidth);
    	
    	params.put("prefix","hr/hr"); //页面路径
    	ModelAndView mv = this.openListPage(params);
    	mv.addObject("hr", "attendanceworkdateset");
    	mv.addObject("hrName", "工作时间设定");
		return mv;
    }
    
	/**
     * 查询所有信息
     * @param params
     * @return TableDataInfo
     * @author lwl
     */
	@GetMapping("/list")
    @RequiresPermissions("hr:attendanceworkdateset:list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
        List<AttendanceWorkDateSet> TalentPoolList = new ArrayList<AttendanceWorkDateSet>();
        try {
        	TalentPoolList = workDateSetService.queryAll(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(TalentPoolList);
    }
    
	/**
     * 修改为上班时间
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @Log(title = "修改为上班时间", businessType = BusinessType.UPDATE)
    @RequiresPermissions("hr:attendanceworkdateset:edit")
    @PostMapping("/editworkday")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editWorkday(@RequestParam Map<String, Object> params)
    {
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= workDateSetService.updateWorkday(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 修改为休息时间
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @Log(title = "修改为休息时间", businessType = BusinessType.UPDATE)
    @RequiresPermissions("hr:attendanceworkdateset:edit")
    @PostMapping("/editholiday")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editHoliday(@RequestParam Map<String, Object> params)
    {
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= workDateSetService.updateHoliday(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
	
	/**
     * 修改时间
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("hr:attendanceworkdateset:edit")
    @GetMapping("/edit")
    public ModelAndView edit(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        params.put("entity", new AttendanceWorkDateSet());
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        AttendanceWorkDateSet workDateSet= new AttendanceWorkDateSet();
        mv = this.gotoModelAndViewErrorPage(params, "guid", "行唯一值");
        if (mv.isEmpty()){
            try{
                dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
                if (dictionaryForFieldMap.isEmpty()){
                    ajaxResult = AjaxResult.error("未获取到字段属性。");
                }else{
                	workDateSet = workDateSetService.queryWorkDateSetByGuid(params.get("guid").toString());
                	if (workDateSet == null){
                		ajaxResult = AjaxResult.error("没有查到该信息。");
                	}else{
                    	ajaxResult = AjaxResult.success("数据获取成功。");
                	}
                }
            }
            catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            mv.addObject("pageType","edit");
            mv.addObject("workDateSet",workDateSet);
            mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
            mv.addObject("ajaxResult", ajaxResult);
            mv.setViewName(prefix + "/attendanceworkdateset_edit");
        }
        return mv;
    }
    
    /**
     * 修改时间--数据提交
     * @param params
     * @return AjaxResult
     * @author lwl
     */
    @Log(title = "修改时间", businessType = BusinessType.UPDATE)
    @RequiresPermissions("hr:attendanceworkdateset:edit")
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(@RequestParam Map<String, Object> params)
    {
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= workDateSetService.update(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 一键生成打卡时间
     * @param 
     * @return 
     * @author DH-Xxj
     */
    @Log(title = "一键生成打卡时间", businessType = BusinessType.INSERT)
    @RequiresPermissions("hr:attendanceworkdateset:add")
    @PostMapping("/insertworkdate")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult insertAll(@RequestParam Map<String, Object> params)
    {
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= workDateSetService.insertWorkDateSet(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}
